<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入表达式名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="default" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="default" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="expressionList" @current-change="handleSingleExpSelect">
      <el-table-column width="55" align="center">
        <template #default="scope">
          <el-radio v-model="radioSelected" :label="scope.row.id">{{ '' }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="表达式内容" align="center" prop="expression" />
      <el-table-column label="表达式类型" align="center" prop="dataType">
        <template #default="scope">
          <dict-tag :options="exp_data_type" :value="scope.row.dataType" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[5, 10]"
      layout="prev, pager, next"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { listExpression } from '@/api/flowable/expression'
import { StrUtil } from '@/utils/StrUtil'

const props = defineProps({
  selectValues: {
    type: [Number, String],
    default: null,
    required: false
  }
})

const emit = defineEmits(['handleSingleExpSelect'])

const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const expressionList = ref([])
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: null,
  expression: null,
  status: null
})
const radioSelected = ref(null)

const { proxy } = getCurrentInstance();
const {exp_data_type} = proxy.useDict('exp_data_type')

watch(() => props.selectValues, (newVal) => {
  if (StrUtil.isNotBlank(newVal)) {
    radioSelected.value = newVal
  }
}, { immediate: true })

onMounted(() => {
  getList()
})

const getList = () => {
  loading.value = true
  listExpression(queryParams.value).then(response => {
    expressionList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

const resetQuery = () => {
  // 这里需要实现重置表单逻辑，具体实现方式可能需要根据你的项目结构调整
  queryParams.value.name = null
  queryParams.value.expression = null
  queryParams.value.status = null
  handleQuery()
}

const handleSingleExpSelect = (selection) => {
  if (selection) {
    radioSelected.value = selection.id
    emit('handleSingleExpSelect', selection)
  }
}
</script>

<style>
/* 隐藏radio展示的label及本身自带的样式 */
.el-radio__label {
  display: none;
}
</style>